clear all
clc

data = readtable('../../data/working/Sample.csv');

data.supply = data.marketcap./(data.open);

ID_unique = unique(data.household_id);

unique_curr = unique(data.currency_id);

for ii=1:length(unique_curr)
    
    temp = unique_curr(ii);
    
    for nn=1:size(data,1)
        
        temp_ind = isequal(temp,data.currency_id(nn));
        
        if temp_ind==1
            
            data.currency_id2(nn) = ii;
            
        end
        
    end
    %ii
end

data.pow = ones(size(data,1),1);

for ii=[1 8]

    temp_ind = (data.currency_id2==ii);
   
    data.pow(temp_ind) = 0;
    
end

%% COLUMN 1

c = readtable('../../data/working/table5_col1.csv');

data.dep_hat = exp(c.logME*data.logME + ...
    c.incomenewdummy*data.incomenewdummy + c.agenewdummy*data.agenewdummy + ...
    c.countrynewdummy*data.countrynewdummy + ...
    c.type_objective*data.type_objective +...
    c.log_libor*data.log_libor + c.log_sp500*data.log_sp500 + c.cons +...
    c.pow*data.pow +...
    c.beta*data.beta + c.momentum4*data.momentum4);

data.w_hat = zeros(size(data,1),1);

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    for cc=1:length(unique_curr)
        
        data.w_hat(temp_ind(cc)) = data.dep_hat(temp_ind(cc))./(1+sum(data.dep_hat(temp_ind)));
        
    end
end

% compute matrices

% G = NaN(length(unique_curr),length(unique_curr),length(ID_unique));
H_mat = NaN(length(unique_curr),length(unique_curr),length(ID_unique));

ela_indiv_prices = NaN(length(unique_curr),length(unique_curr),length(ID_unique));

ela_indiv.prices = NaN(length(ID_unique),length(unique_curr));

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    temp_w = data.w_hat(temp_ind);
    
    G = diag(temp_w) - temp_w*temp_w';
    
    H_mat(:,:,ii) = data.wealth(temp_ind(1))*diag(temp_w);
    
    if any(isnan(temp_w))==0
        ela_indiv_temp = -(eye(length(unique_curr)) - c.logME*pinv(diag(temp_w))*G);
    else ela_indiv_temp = NaN;
    end
    
    ela_indiv_prices(:,:,ii) = ela_indiv_temp;
    
    ela_indiv.prices(ii,:) = diag(ela_indiv_prices(:,:,ii));
    
end

nanmean(ela_indiv_prices,3);

H = nansum(H_mat,3);

ela_second_term_mat = zeros(length(unique_curr),length(unique_curr),length(ID_unique));

for ii=1:length(ID_unique)

    temp_ind = find(data.household_id==ID_unique(ii));
    
    temp_w = data.w_hat(temp_ind);
    
    G = diag(temp_w) - temp_w*temp_w';
    
    ela_second_term_mat(:,:,ii) = c.logME*data.wealth(temp_ind(1))*(H\G);
    
end

ela_second_term = nansum(ela_second_term_mat,3);

ela_aggr_prices = -(eye(length(unique_curr)) - ela_second_term);

mean(diag(ela_aggr_prices))

%% COLUMN 2

c = readtable('../../data/working/table5_col2.csv');

data.dep_hat = exp(c.logME*data.logME + ...
    c.short_increase*data.short_increase + ...
    c.incomenewdummy*data.incomenewdummy + c.agenewdummy*data.agenewdummy + ...
    c.countrynewdummy*data.countrynewdummy + ...
    c.type_objective*data.type_objective +...
    c.log_libor*data.log_libor + c.log_sp500*data.log_sp500 + c.cons +...
    c.pow*data.pow +...
    c.beta*data.beta + c.momentum4*data.momentum4);

data.w_hat = zeros(size(data,1),1);

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    for cc=1:length(unique_curr)
        
        data.w_hat(temp_ind(cc)) = data.dep_hat(temp_ind(cc))./(1+sum(data.dep_hat(temp_ind)));
        
    end
end

% compute matrices

% G = NaN(length(unique_curr),length(unique_curr),length(ID_unique));
H_mat = NaN(length(unique_curr),length(unique_curr),length(ID_unique));

ela_indiv_prices = NaN(length(unique_curr),length(unique_curr),length(ID_unique));

ela_indiv.prices = NaN(length(ID_unique),length(unique_curr));

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    temp_w = data.w_hat(temp_ind);
    
    G = diag(temp_w) - temp_w*temp_w';
    
    H_mat(:,:,ii) = data.wealth(temp_ind(1))*diag(temp_w);
    
    if any(isnan(temp_w))==0
        ela_indiv_temp = -(eye(length(unique_curr)) - c.logME*pinv(diag(temp_w))*G);
    else ela_indiv_temp = NaN;
    end
    
    ela_indiv_prices(:,:,ii) = ela_indiv_temp;
    
    ela_indiv.prices(ii,:) = diag(ela_indiv_prices(:,:,ii));
    
end

nanmean(ela_indiv_prices,3);

H = nansum(H_mat,3);

ela_second_term_mat = zeros(length(unique_curr),length(unique_curr),length(ID_unique));

for ii=1:length(ID_unique)

    temp_ind = find(data.household_id==ID_unique(ii));
    
    temp_w = data.w_hat(temp_ind);
    
    G = diag(temp_w) - temp_w*temp_w';
    
    ela_second_term_mat(:,:,ii) = c.logME*data.wealth(temp_ind(1))*(H\G);
    
end

ela_second_term = nansum(ela_second_term_mat,3);

ela_aggr_prices = -(eye(length(unique_curr)) - ela_second_term);

mean(diag(ela_aggr_prices))


%% COLUMN 3

c = readtable('../../data/working/table5_col3.csv');

data.dep_hat = exp(c.logME*data.logME + ...
    c.short_increase*data.short_increase + ...
    c.long_decrease*data.long_decrease + ...
    c.incomenewdummy*data.incomenewdummy + c.agenewdummy*data.agenewdummy + ...
    c.countrynewdummy*data.countrynewdummy + ...
    c.type_objective*data.type_objective +...
    c.log_libor*data.log_libor + c.log_sp500*data.log_sp500 + c.cons +...
    c.pow*data.pow +...
    c.beta*data.beta + c.momentum4*data.momentum4);

data.w_hat = zeros(size(data,1),1);

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    for cc=1:length(unique_curr)
        
        data.w_hat(temp_ind(cc)) = data.dep_hat(temp_ind(cc))./(1+sum(data.dep_hat(temp_ind)));
        
    end
end

% compute matrices

% G = NaN(length(unique_curr),length(unique_curr),length(ID_unique));
H_mat = NaN(length(unique_curr),length(unique_curr),length(ID_unique));

ela_indiv_prices = NaN(length(unique_curr),length(unique_curr),length(ID_unique));

ela_indiv.prices = NaN(length(ID_unique),length(unique_curr));

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    temp_w = data.w_hat(temp_ind);
    
    G = diag(temp_w) - temp_w*temp_w';
    
    H_mat(:,:,ii) = data.wealth(temp_ind(1))*diag(temp_w);
    
    if any(isnan(temp_w))==0
        ela_indiv_temp = -(eye(length(unique_curr)) - c.logME*pinv(diag(temp_w))*G);
    else ela_indiv_temp = NaN;
    end
    
    ela_indiv_prices(:,:,ii) = ela_indiv_temp;
    
    ela_indiv.prices(ii,:) = diag(ela_indiv_prices(:,:,ii));
    
end

nanmean(ela_indiv_prices,3);

H = nansum(H_mat,3);

ela_second_term_mat = zeros(length(unique_curr),length(unique_curr),length(ID_unique));

for ii=1:length(ID_unique)

    temp_ind = find(data.household_id==ID_unique(ii));
    
    temp_w = data.w_hat(temp_ind);
    
    G = diag(temp_w) - temp_w*temp_w';
    
    ela_second_term_mat(:,:,ii) = c.logME*data.wealth(temp_ind(1))*(H\G);
    
end

ela_second_term = nansum(ela_second_term_mat,3);

ela_aggr_prices = -(eye(length(unique_curr)) - ela_second_term);

mean(diag(ela_aggr_prices))


%% COLUMN 4

c = readtable('../../data/working/table5_col4.csv');

data.dep_hat = exp(c.logME*data.logME + ...
    c.short_increase*data.short_increase + ...
    c.long_decrease*data.long_decrease + ...
    c.currencies_potential*data.currencies_potential + ...
    c.incomenewdummy*data.incomenewdummy + c.agenewdummy*data.agenewdummy + ...
    c.countrynewdummy*data.countrynewdummy + ...
    c.type_objective*data.type_objective +...
    c.log_libor*data.log_libor + c.log_sp500*data.log_sp500 + c.cons +...
    c.pow*data.pow +...
    c.beta*data.beta + c.momentum4*data.momentum4);

data.w_hat = zeros(size(data,1),1);

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    for cc=1:length(unique_curr)
        
        data.w_hat(temp_ind(cc)) = data.dep_hat(temp_ind(cc))./(1+sum(data.dep_hat(temp_ind)));
        
    end
end

% compute matrices

% G = NaN(length(unique_curr),length(unique_curr),length(ID_unique));
H_mat = NaN(length(unique_curr),length(unique_curr),length(ID_unique));

ela_indiv_prices = NaN(length(unique_curr),length(unique_curr),length(ID_unique));

ela_indiv.prices = NaN(length(ID_unique),length(unique_curr));

for ii=1:length(ID_unique)
    
    temp_ind = find(data.household_id==ID_unique(ii));
        
    temp_w = data.w_hat(temp_ind);
    
    G = diag(temp_w) - temp_w*temp_w';
    
    H_mat(:,:,ii) = data.wealth(temp_ind(1))*diag(temp_w);
    
    if any(isnan(temp_w))==0
        ela_indiv_temp = -(eye(length(unique_curr)) - c.logME*pinv(diag(temp_w))*G);
    else ela_indiv_temp = NaN;
    end
    
    ela_indiv_prices(:,:,ii) = ela_indiv_temp;
    
    ela_indiv.prices(ii,:) = diag(ela_indiv_prices(:,:,ii));
    
end

nanmean(ela_indiv_prices,3);

H = nansum(H_mat,3);

ela_second_term_mat = zeros(length(unique_curr),length(unique_curr),length(ID_unique));

for ii=1:length(ID_unique)

    temp_ind = find(data.household_id==ID_unique(ii));
    
    temp_w = data.w_hat(temp_ind);
    
    G = diag(temp_w) - temp_w*temp_w';
    
    ela_second_term_mat(:,:,ii) = c.logME*data.wealth(temp_ind(1))*(H\G);
    
end

ela_second_term = nansum(ela_second_term_mat,3);

ela_aggr_prices = -(eye(length(unique_curr)) - ela_second_term);

mean(diag(ela_aggr_prices))
